package com.pic.mgt.action.shell;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;

import com.pic.mgt.action.AutoInspectMissUnitAction;
import com.pic.mgt.action.ScheduleAutoInspectMissUnitAction;
import com.pic.mgt.telnetd.AbstractCommandPerformer;

public class AutoInspectMissUnitCommandPerformer extends
		AbstractCommandPerformer {
	
	private final String commandName = "rcvall";
	private final String commandDescritpion = "auto-inspect miss or halt unit(both business and message) and do recovery. (notice: it is suggested using with '-dy'.               see the '-dy' description.)";
	
	private AutoInspectMissUnitAction autoInspectMissUnitAction;
	private ScheduleAutoInspectMissUnitAction scheduleAutoInspectMissUnitAction;
	
	@Override
	protected void addOption(Options options) {
		options.addOption("dy", false, "waiting for a system delay time, and then do auto-inspection. (opt.) (*notice: Before doing this process, make sure that unit monitor was started. So that auto-inspect process will not omit any miss units.)");
	}

	@Override
	protected String doCommandPerform(CommandLine commandLine) {
		if (commandLine.hasOption("dy")) {
			// 表示要 delay 一段系統時間後,再做動作-> 所以加入排程
			
			boolean isScheduleAutoRecoveryUnitSuccessful =
				this.scheduleAutoInspectMissUnitAction.scheduleAutoInspectMissUnitAndRecover();
			
			if (isScheduleAutoRecoveryUnitSuccessful) {
				return super.getCommandPerformFinishMsg();
			}
			else {
				return super.getCommandPerformFailedMsg();
			}
		}
		else {
		   // 表示要立即做
			
			boolean isAutoRecoveryUnitSuccessful =
				this.autoInspectMissUnitAction.autoInspectMissUnitAndRecover();
			
			if (isAutoRecoveryUnitSuccessful) {
				return super.getCommandPerformFinishMsg();
			}
			else {
				return super.getCommandPerformFailedMsg();
			}
		}
	}

	@Override
	public String getCommandDescription() {
		return this.commandDescritpion;
	}

	@Override
	public String getCommandName() {
		return this.commandName;
	}

	// --- getter and setter ---
	
	public void setAutoInspectMissUnitAction(
			AutoInspectMissUnitAction autoInspectMissUnitAction) {
		this.autoInspectMissUnitAction = autoInspectMissUnitAction;
	}

	public void setScheduleAutoInspectMissUnitAction(
			ScheduleAutoInspectMissUnitAction scheduleAutoInspectMissUnitAction) {
		this.scheduleAutoInspectMissUnitAction = scheduleAutoInspectMissUnitAction;
	}

}
